package com.course.web.component.custom;

import com.course.domain.model.TaskClass;
import com.course.domain.model.TaskStory;
import com.course.web.biz.WebTaskBiz;
import com.course.web.component.base.BaseEdit;
import com.course.web.component.base.BaseGrid;
import com.course.web.component.base.EditGrid;
import com.course.web.model.*;
import com.course.web.view.CourseListView;
import com.vaadin.data.Binder;
import com.vaadin.data.converter.StringToIntegerConverter;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Justin on 2017/6/11.
 */
@SpringComponent
@ViewScope
public class TaskDataGrid extends CustomComponent {

    private final WebTaskBiz taskBiz;

    private final TaskAudioGrid audioGrid;
    private final TaskVideoGrid videoGrid;
    private final TaskStoryGrid storyGrid;
    private final TaskClassGrid classGrid;
    private final TaskEventGrid eventGrid;

    private int courseId;

    public TaskDataGrid(WebTaskBiz taskBiz) {
        this.taskBiz = taskBiz;

        this.audioGrid = new TaskAudioGrid();
        this.videoGrid = new TaskVideoGrid();
        this.storyGrid = new TaskStoryGrid();
        this.classGrid = new TaskClassGrid();
        this.eventGrid = new TaskEventGrid();

        init();
    }

    public void enter(int courseId) {
        this.courseId = courseId;
        audioGrid.init(taskBiz.findAudio(courseId));
        videoGrid.init(taskBiz.findVideo(courseId));
        storyGrid.init(taskBiz.findStory(courseId));
        classGrid.init(taskBiz.findClass(courseId));
        eventGrid.init(taskBiz.findEvent(courseId));
    }

    private void init() {
        Layout root = new VerticalLayout();
        Layout menu = new HorizontalLayout();
        Button back = new Button("返回");
        TabSheet sheet = new TabSheet();

        root.addComponent(menu);
        root.addComponent(sheet);
        menu.addComponent(back);

        setCompositionRoot(root);
        setSizeFull();

        sheet.addTab(audioGrid, "音频");
        sheet.addTab(videoGrid, "视频");
        sheet.addTab(storyGrid, "故事");
        sheet.addTab(classGrid, "教学");
        sheet.addTab(eventGrid, "线下");

        back.addClickListener(event -> getUI().getNavigator().navigateTo(CourseListView.NAME));
    }

    private class TaskBasicGrid<T> extends EditGrid<T> {

        TaskBasicGrid(Class<T> type) {
            super(type);
        }

        @Override
        protected void doInitMenu(Layout menu) {
            Button createBtn = new Button("添加");
            menu.addComponent(createBtn);
            createBtn.addClickListener(event -> createShow());
        }
    }


    private class TaskAudioGrid extends TaskBasicGrid<TaskAudioVM> {

        TaskAudioGrid() {
            super(TaskAudioVM.class);
        }

        @Override
        protected void doInitGrid(BaseGrid<TaskAudioVM> grid) {
            grid.addColumn(TaskAudioVM::getId, "ID");
            grid.addColumn(TaskAudioVM::getTitle, "音频标题");
            grid.addColumn(TaskAudioVM::getTimes, "重复次数");
            grid.addColumn(TaskAudioVM::getSpeed, "播放速度");
            grid.addColumn(TaskAudioVM::getDetail, "音频详情");
//            grid.addColumn(TaskAudioVM::getAudioLink, "音频文件");
//            grid.addColumn(TaskAudioVM::getAlbumLink, "封面文件");
            grid.addButton(this::updateShow, "编辑");
        }

        @Override
        protected void doInitEdit(BaseEdit<TaskAudioVM> edit) {
            FormLayout form = new FormLayout();
            TextField title = new TextField("音频标题");
            TextField media = new TextField("音频文件");
            TextField album = new TextField("封面文件");
            TextField times = new TextField("重复次数");
            TextField speed = new TextField("播放速度");
            TextArea detail = new TextArea("音频详情");
            form.addComponent(title);
            form.addComponent(media);
            form.addComponent(album);
            form.addComponent(times);
            form.addComponent(speed);
            form.addComponent(detail);
            form.setMargin(false);
            edit.initEdit(form);

            Binder<TaskAudioVM> binder = edit.binder();
            StringToIntegerConverter converter = new StringToIntegerConverter("输入数字");
            binder.forField(title).bind("title");
            binder.forField(media).bind("audioLink");
            binder.forField(album).bind("albumLink");
            binder.forField(times).withConverter(converter).bind("times");
            binder.forField(speed).withConverter(converter).bind("speed");
            binder.forField(detail).bind("detail");
        }

        @Override
        protected TaskAudioVM doCreate() {
            TaskAudioVM vm = new TaskAudioVM(courseId);
            vm.setTimes(1);
            vm.setSpeed(100);
            return vm;
        }

        @Override
        protected TaskAudioVM doCreate(TaskAudioVM item) {
            return taskBiz.createAudio(item);
        }

        @Override
        protected TaskAudioVM doUpdate(TaskAudioVM item) {
            return taskBiz.updateAudio(item);
        }
    }

    private class TaskVideoGrid extends TaskBasicGrid<TaskVideoVM> {

        TaskVideoGrid() {
            super(TaskVideoVM.class);
        }

        @Override
        protected void doInitGrid(BaseGrid<TaskVideoVM> grid) {
            grid.addColumn(TaskVideoVM::getId, "ID");
            grid.addColumn(TaskVideoVM::getTitle, "视频标题");
            grid.addColumn(TaskVideoVM::getTimes, "重复次数");
            grid.addColumn(TaskVideoVM::getDetail, "视频详情");
//            grid.addColumn(TaskVideoVM::getVideoLink, "视频文件");
//            grid.addColumn(TaskVideoVM::getAlbumLink, "封面文件");
            grid.addButton(this::updateShow, "编辑");
        }

        @Override
        protected void doInitEdit(BaseEdit<TaskVideoVM> edit) {
            FormLayout form = new FormLayout();
            TextField title = new TextField("视频标题");
            TextField media = new TextField("视频文件");
            TextField album = new TextField("封面文件");
            TextField times = new TextField("重复次数");
            TextArea detail = new TextArea("视频详情");
            form.addComponent(title);
            form.addComponent(media);
            form.addComponent(album);
            form.addComponent(times);
            form.addComponent(detail);
            form.setMargin(false);
            edit.initEdit(form);

            Binder<TaskVideoVM> binder = edit.binder();
            StringToIntegerConverter converter = new StringToIntegerConverter("输入数字");
            binder.forField(title).bind("title");
            binder.forField(media).bind("videoLink");
            binder.forField(album).bind("albumLink");
            binder.forField(times).withConverter(converter).bind("times");
            binder.forField(detail).bind("detail");
        }

        @Override
        protected TaskVideoVM doCreate() {
            TaskVideoVM vm = new TaskVideoVM(courseId);
            vm.setTimes(1);
            return vm;
        }

        @Override
        protected TaskVideoVM doCreate(TaskVideoVM item) {
            return taskBiz.createVideo(item);
        }

        @Override
        protected TaskVideoVM doUpdate(TaskVideoVM item) {
            return taskBiz.updateVideo(item);
        }
    }

    private class TaskStoryGrid extends TaskBasicGrid<TaskStoryVM> {

        TaskStoryGrid() {
            super(TaskStoryVM.class);
        }

        @Override
        protected void doInitGrid(BaseGrid<TaskStoryVM> grid) {
            grid.addColumn(TaskStoryVM::getId, "ID");
            grid.addColumn(TaskStoryVM::getTitle, "故事标题");
            grid.addColumn(TaskStoryVM::getTimes, "重复次数");
            grid.addColumn(TaskStoryVM::getSpeed, "播放速度");
            grid.addColumn(TaskStoryVM::getState, "看书状态");
            grid.addColumn(TaskStoryVM::getDetail, "故事详情");
//            grid.addColumn(TaskStoryVM::getStoryLink, "音频文件");
//            grid.addColumn(TaskStoryVM::getAlbumLink, "封面文件");
            grid.addButton(this::updateShow, "编辑");
        }

        @Override
        protected void doInitEdit(BaseEdit<TaskStoryVM> edit) {
            FormLayout form = new FormLayout();
            TextField title = new TextField("故事标题");
            TextField story = new TextField("音频文件");
            TextField album = new TextField("封面文件");
            RadioButtonGroup<TaskStory.BookState> bookState = newTypeRatio("看书状态");
            TextField times = new TextField("重复次数");
            TextField speed = new TextField("播放速度");
            TextArea detail = new TextArea("故事内容");
            form.addComponent(title);
            form.addComponent(story);
            form.addComponent(album);
            form.addComponent(bookState);
            form.addComponent(times);
            form.addComponent(speed);
            form.addComponent(detail);
            form.setMargin(false);
            edit.initEdit(form);

            Binder<TaskStoryVM> binder = edit.binder();
            StringToIntegerConverter converter = new StringToIntegerConverter("输入数字");
            binder.forField(title).bind("title");
            binder.forField(story).bind("storyLink");
            binder.forField(album).bind("albumLink");
            binder.forField(bookState).bind("bookState");
            binder.forField(times).withConverter(converter).bind("times");
            binder.forField(speed).withConverter(converter).bind("speed");
            binder.forField(detail).bind("detail");
        }

        @Override
        protected TaskStoryVM doCreate() {
            TaskStoryVM vm = new TaskStoryVM(courseId);
            vm.setTimes(1);
            vm.setSpeed(100);
            return vm;
        }

        @Override
        protected TaskStoryVM doCreate(TaskStoryVM item) {
            return taskBiz.createStory(item);
        }

        @Override
        protected TaskStoryVM doUpdate(TaskStoryVM item) {
            return taskBiz.updateStory(item);
        }

        private RadioButtonGroup<TaskStory.BookState> newTypeRatio(String caption) {
            List<TaskStory.BookState> options = Arrays.asList(TaskStory.BookState.WITH_BOOK, TaskStory.BookState.WITHOUT_BOOK);
            RadioButtonGroup<TaskStory.BookState> group = new RadioButtonGroup<>(caption, options);
            group.setItemCaptionGenerator(TaskStory.BookState::getTitle);
            group.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
            return group;
        }
    }

    private class TaskClassGrid extends TaskBasicGrid<TaskClassVM> {

        TaskClassGrid() {
            super(TaskClassVM.class);
        }

        @Override
        protected void doInitGrid(BaseGrid<TaskClassVM> grid) {
            grid.addColumn(TaskClassVM::getId, "ID");
            grid.addColumn(TaskClassVM::getTitle, "教学标题");
            grid.addColumn(TaskClassVM::getMinutes, "教学时长(分钟)");
            grid.addColumn(TaskClassVM::getType, "老师类型");
            grid.addColumn(TaskClassVM::getDetail, "教学内容");
            grid.addButton(this::updateShow, "编辑");
        }

        @Override
        protected void doInitEdit(BaseEdit<TaskClassVM> edit) {
            FormLayout form = new FormLayout();
            TextField title = new TextField("教学标题");
            TextField minutes = new TextField("教学时长(分钟)");
            RadioButtonGroup<TaskClass.Type> type = newTypeRatio("老师类型");
            TextArea detail = new TextArea("教学内容");
            TextField pptLink = new TextField("PPT链接");
            form.addComponent(title);
            form.addComponent(detail);
            form.addComponent(minutes);
            form.addComponent(type);
            form.addComponent(pptLink);
            form.setMargin(false);
            edit.initEdit(form);

            Binder<TaskClassVM> binder = edit.binder();
            StringToIntegerConverter converter = new StringToIntegerConverter("输入数字");
            binder.forField(title).bind("title");
            binder.forField(type).bind("classType");
            binder.forField(minutes).withConverter(converter).bind("minutes");
            binder.forField(detail).bind("detail");
            binder.forField(pptLink).bind("pptLink");
        }

        @Override
        protected TaskClassVM doCreate() {
            TaskClassVM vm = new TaskClassVM(courseId);
            vm.setMinutes(10);
            return vm;
        }

        @Override
        protected TaskClassVM doCreate(TaskClassVM item) {
            return taskBiz.createClass(item);
        }

        @Override
        protected TaskClassVM doUpdate(TaskClassVM item) {
            return taskBiz.updateClass(item);
        }

        private RadioButtonGroup<TaskClass.Type> newTypeRatio(String caption) {
            List<TaskClass.Type> options = Arrays.asList(TaskClass.Type.CHINESE, TaskClass.Type.ENGLISH);
            RadioButtonGroup<TaskClass.Type> group = new RadioButtonGroup<>(caption, options);
            group.setItemCaptionGenerator(TaskClass.Type::getTitle);
            group.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
            return group;
        }
    }

    private class TaskEventGrid extends TaskBasicGrid<TaskEventVM> {

        TaskEventGrid() {
            super(TaskEventVM.class);
        }

        @Override
        protected void doInitGrid(BaseGrid<TaskEventVM> grid) {
            grid.addColumn(TaskEventVM::getId, "ID");
            grid.addColumn(TaskEventVM::getTitle, "课程标题");
            grid.addColumn(TaskEventVM::getMinutes, "课程时长(分钟)");
            grid.addColumn(TaskEventVM::getDetail, "课程内容");
            grid.addButton(this::updateShow, "编辑");
        }

        @Override
        protected void doInitEdit(BaseEdit<TaskEventVM> edit) {
            FormLayout form = new FormLayout();
            TextField title = new TextField("课程标题");
            TextField minutes = new TextField("课程时长(分钟)");
            TextArea detail = new TextArea("课程内容");
            form.addComponent(title);
            form.addComponent(minutes);
            form.addComponent(detail);
            form.setMargin(false);
            edit.initEdit(form);

            Binder<TaskEventVM> binder = edit.binder();
            StringToIntegerConverter converter = new StringToIntegerConverter("输入数字");
            binder.forField(title).bind("title");
            binder.forField(minutes).withConverter(converter).bind("minutes");
            binder.forField(detail).bind("detail");
        }

        @Override
        protected TaskEventVM doCreate() {
            TaskEventVM vm = new TaskEventVM(courseId);
            vm.setMinutes(60);
            return vm;
        }

        @Override
        protected TaskEventVM doCreate(TaskEventVM item) {
            return taskBiz.createEvent(item);
        }

        @Override
        protected TaskEventVM doUpdate(TaskEventVM item) {
            return taskBiz.updateEvent(item);
        }
    }
}
